﻿using System.Net;
using System.Text;

namespace GmControlTools
{
    public class CommonFun
    {
        /// <summary>
        /// HTTP POST方式请求数据
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="param">POST的数据</param>
        /// <returns></returns>
        public static string HttpPostRaw(string url, string param)
        {
            //创建httpWebRequest对象
            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
            System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
            if (httpRequest == null)
            {
                Console.WriteLine(string.Format("Invalid url string: {0}", url));
            }
            //填充httpWebRequest的基本信息
            httpRequest.ContentType = "application/x-www-form-urlencoded; encoding=utf-8";
            httpRequest.Method = "POST";
            Encoding encoding = System.Text.Encoding.UTF8;
            byte[] bytesToPost = encoding.GetBytes(param);

            //填充并发送要post的内容
            httpRequest.ContentLength = bytesToPost.Length;
            Stream requestStream = httpRequest.GetRequestStream();
            requestStream.Write(bytesToPost, 0, bytesToPost.Length);
            requestStream.Close();

            //发送post请求到服务器并读取服务器返回信息
            Stream responseStream = null; ;
            try
            {
                responseStream = httpRequest.GetResponse().GetResponseStream();
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Invalid Server Error: {0}", e.Message));
            }
            //读取服务器返回信息
            string stringResponse = string.Empty;
            using (StreamReader responseReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
            {
                stringResponse = responseReader.ReadToEnd();
            }
            responseStream.Close();
            return stringResponse;
        }

        /// <summary>
        /// HTTP GET方式请求数据.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            HttpWebRequest? request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "*/*";
            WebResponse? response = null;
            string? responseStr = "";
            try
            {
                response = request.GetResponse();

                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                request = null;
                response = null;
            }

            return responseStr;
        }

    }
}
